package D119;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.01.19.10.15
 */
public class Pj2 {

    public static void main(String[] args) {
         String f="";
        System.out.println(head(f));
        Map<String,String> map=new HashMap<>();
        map.put("ffd8ffe0","jpg");
    }

    public static  String ext(String file){
        String ext="未知";
        Map<String,String> map=new HashMap<>();
        map.put("ffd8ffe0","jpg");
        String head=head(file);
        head=head.substring(0,head.lastIndexOf(":"));
        if(map.containsKey(head)){
            ext=map.get(head);
        }else {
            ext="未知类型";
        }
        return ext;
    }




    public static String head(String file){
        return head(new File(file));
    }
    //获取文件头信息
    public static String head(File file) {
        String info = "未知";
        try (var f = new FileInputStream(file)) {
            byte[] buf = new byte[8];
            f.read(buf);
            StringBuilder sbu = new StringBuilder();
            for (byte b : buf) {
                sbu.append(String.format("%02x", b));
            }
            String filename = file.getName();
            String ext = filename.lastIndexOf(".") != -1 ? filename.substring(filename.lastIndexOf(".") + 1) : "unkowm";
            info = sbu.toString() + ":" + ext;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return info;
    }

}
